package cn.jbolt.ec.commodity;

import cn.jbolt._admin.permission.CheckPermission;
import cn.jbolt._admin.permission.PermissionKey;
import cn.jbolt._admin.permission.UnCheckIfSystemAdmin;
import cn.jbolt.base.JBoltBaseController;
import cn.jbolt.common.bean.OptionBean;
import cn.jbolt.common.config.Msg;
import cn.jbolt.common.config.JBoltUploadFolder;
import cn.jbolt.common.model.BecRetailer;
import cn.jbolt.common.model.BecRetailerCommodity;
import cn.jbolt.common.model.BecRetailerCommodityOption;
import cn.jbolt.common.model.BecRetailerCommodityType;
import cn.jbolt.ec.util.MyUtil;
import com.jfinal.aop.Inject;
import com.jfinal.core.JFinal;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

import java.util.ArrayList;
import java.util.List;

@CheckPermission(PermissionKey.SPECIAL_COMMODITY_ADMIN)
public class EcSpecialCommodityAuthController extends JBoltBaseController {
    @Inject
    CommodityService service;
    @Inject
    CommodityOptionService optionService;
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_INDEX)
    public void index(){

        try {
            String status=get("Status");
            if (status==null||status=="") status="!=0";
            else status="="+status;
            Page<BecRetailerCommodity> pageData=service.paginateAdminData(getPageNumber(),getPageSize(),getKeywords(),getInt("onSale"),getBoolean("isDelete",false),getInt("commodityTypeId"),getInt("onSelectType"),getInt("onSelectCommodityType"),status);
            keepPara();
            set("deleteCount1", service.getDeleteCount());
            set("pageData1", pageData);
            render("index.html");
        }catch (Exception e){

        }




    }
    /**
     *  预备添加 界面
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_ADD)
    public void preadd(){
        render("choose_commodity_type.html");
    }

    /**
     * 更新后端分类
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_MODIFY)
    public void update_commodity_type(){
        Integer commodity_id=getInt("commodity_id");
        Integer commodity_type_id=getInt("commodity_type_id");
        Ret ret=service.updateBackType(getSessionAdminUserId(),commodity_id,commodity_type_id);
        if(ret.isFail()){
            renderErrorPjax(ret.getStr("msg"), "javascript:goback()");
            return;
        }
        //直接redirect到edit中
        redirect("/auth/mall/special_commodity/edit_commodity/"+commodity_id);
    }


    /**
     * 进入添加新商品
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_ADD)
    public void add_commodity(){
        Ret ret=service.create_commodity(getSessionAdminUserId(),getInt("commodity_type_id"));
        if(ret.isFail()){
            renderErrorPjax(ret.getStr("msg"), "javascript:goback()");
            return;
        }
        redirect("/auth/mall/special_commodity/edit_commodity/"+ret.getInt("data"));
    }

    /**
     * 进入商品编辑
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_MODIFY)
    public void edit_commodity(){
        Integer CommodityId=getInt(0);
        if(notOk(CommodityId)){
            renderErrorPjax(Msg.PARAM_ERROR, "javascript:goback()");
            return;
        }
        BecRetailerCommodity becRetailerCommodity=service.get_commodity_with_option(CommodityId);
        System.out.println("2");
        if(becRetailerCommodity==null){
            renderErrorPjax("商品信息不存在", "javascript:goback()");
            return;
        }
        Ret ret=service.check_can_edit_commodity(becRetailerCommodity);
        if(ret.isFail()){
            set("failTips", ret.get("msg"));
        }
        set("commodity", becRetailerCommodity);
        render("form.html");
    }

    /**
     * 更新商品基本信息
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_MODIFY)
    public void update_commodity_base(){
        renderJson(service.update_commodity_base(getSessionAdminUserId(),getModel(BecRetailerCommodity.class,"Commodity"),get("price"),get("special_price")));
    }

    /**
     * 软删除商品
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_DELETE)
    public void delete_commodity(){
        renderJson(service.delete_commodity(getSessionAdminUserId(),getInt(0)));
    }


    /**
     * select onSale下拉数据源
     */
    public void on_sale(){
        List<OptionBean> options=new ArrayList<OptionBean>();
        options.add(new OptionBean("待上架",3));
        options.add(new OptionBean("未上架",2));
        options.add(new OptionBean("已上架",1));
        renderJsonData(options);
    }


    /**
     * 获取商家下拉数据源
     */
    public void on_select_type(){
        List<OptionBean> options=new ArrayList<OptionBean>();
        BecRetailer becRetailer = new BecRetailer();
        List<BecRetailer> list= becRetailer.dao().findAll();
        if (list==null){
            return;
        }
        for(BecRetailer list1:list){
            options.add(new OptionBean(list1.getName(),list1.getId()));
        }
        renderJsonData(options);
    }

    /**
     * 获取商品顶级类型下拉数据源
     */
    public void on_select_commodity_type(){
        List<OptionBean> options=new ArrayList<OptionBean>();
        BecRetailerCommodityType becRetailerCommodityType = new BecRetailerCommodityType();
        List<BecRetailerCommodityType> list= becRetailerCommodityType.dao().find("select * from bec_retailer_commodity_type where pid = 0");
        if (list==null){
            return;
        }
        for(BecRetailerCommodityType list1:list){
            options.add(new OptionBean(list1.getName(),list1.getId()));
        }
        renderJsonData(options);
    }

    /**
     * 切换上下架
     */
    public void toggle_on_sale(){
        renderJson(service.toggle_on_sale(getSessionAdminUserId(),getInt(0)));
    }


    /**
     * 进入修改分类界面
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_MODIFY)
    public void edit_back_Type() {
        Integer commodityId = getInt(0);
        if (notOk(commodityId)) {
            renderErrorPjax(Msg.PARAM_ERROR, "javascript:goback()");
            return;
        }
        BecRetailerCommodity becRetailerCommodity = service.findById(commodityId);
        if (becRetailerCommodity == null) {
            renderErrorPjax("商品信息不存在", "javascript:goback()");
            return;
        }
        String backfullname="";
        BecRetailerCommodityType becRetailerCommodityType= MyUtil.me.get_Commodity_type(becRetailerCommodity.getTypeId());
        backfullname=String.valueOf(becRetailerCommodityType.getId());
        while (true){
            if (becRetailerCommodityType.getPid()==0){
                break;
            }
            becRetailerCommodityType= MyUtil.me.get_Commodity_type(becRetailerCommodityType.getPid());
            backfullname=becRetailerCommodityType.getId()+"_"+backfullname;
        }
        Ret ret = service.check_can_edit_commodity(becRetailerCommodity);
        System.out.println("Bbbbb" + ret);
        if (ret.isOk()) {
            set("CommodityName", becRetailerCommodity.getName());
            set("commodity_full_name", MyUtil.me.get_commodity_full_name(becRetailerCommodity.getTypeId()));
            set("commodity_id", becRetailerCommodity.getId());
            set("commodity_type_id", becRetailerCommodity.getTypeId());
            set("back_commodity_key", backfullname);
            render("choose_commodity_type.html");
        } else {
            renderErrorPjax(ret.getStr("msg"), "javascript:goback()");
        }
    }

    /**
     * 恢复回收站里的一个商品
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_RESTORE)
    public void restore_commodity(){
        renderJson(service.restore_commodity(getSessionAdminUserId(),getInt(0)));
    }

    /**
     * 更新商品图文详情信息(富文本框修改) (商品介绍)
     */

    public void update_text_commodity(){
        renderJson(service.update_text_commodity(getModel(BecRetailerCommodity.class,"commodityHtmlContent")));
    }

    /**
     * 对上传图片进行加工处理
     */
    public void upload_image(){
        String todayFolder= JBoltUploadFolder.todayFolder();
        String uploadPath="/"+todayFolder;
        UploadFile file=getFile("file",uploadPath);
        String key= file.getUploadPath()+"/"+file.getFileName();
        if (key==null){
            renderJsonFail("操作失败");
        }
        renderJsonData(key);

    }

    /**
     * 保存content圖片
     */
    public void upload_content_image(){
        Integer id=getInt(0);
        String todayFolder= JBoltUploadFolder.todayFolder();
        String uploadPath="/"+todayFolder;
        UploadFile file=getFile("file",uploadPath);
        String key= file.getUploadPath()+"/"+file.getFileName();
        System.out.println(key);
        if (key==null){
            renderJsonFail("操作失败");
        }
        String localUrl= JFinal.me().getConstants().getBaseUploadPath()+"/"+uploadPath+"/"+file.getFileName();
        service.save_content_image(key,file,uploadPath);


        renderJson(service.save_content_image(key,file,uploadPath));

    }
    /**
     * 更新商品图片信息
     */
    public void update_images(){
        renderJson(service.updateImages(getSessionAdminUserId(),getModel(BecRetailerCommodity.class,"Commodity_image")));
    }

    /**
     *新增商品详情
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_ADD)
    public void save_commodity_option(){

        renderJson(optionService.save_commodity_options(getModel(BecRetailerCommodityOption.class,"option"),get("price"),get("special_price")));
    }

    /**
     * 将商品详情传给update_option页面
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_ADD)
    public void content_update_commodity(){
        Integer memberid=getInt(0);
        if(notOk(memberid)){
            renderErrorPjax(Msg.PARAM_ERROR, "javascript:goback()");
            return;
        }
        BecRetailerCommodityOption becRetailerCommodityOption=optionService.findById(memberid);

        if(becRetailerCommodityOption==null){
            renderErrorPjax("商品信息不存在", "javascript:goback()");
            return;
        }
        set("CommodityOption",becRetailerCommodityOption);
        render("update_option.html");
    }

    /**
     * 修改商品详情
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_MODIFY)
    public void update_option(){
        System.out.println("调用了吗");
        System.out.println(getModel(BecRetailerCommodityOption.class,"Bec_option"));
        renderJsonData(optionService.update_option(getSessionAdminUserId(),getModel(BecRetailerCommodityOption.class,"Bec_option"),get("price"),get("special_price")));

    }

    /**
     * 软删除商品详情
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_DELETE)
    public void delete_option(){
        Integer id=getInt(0);
        renderJson(optionService.delete_option(id));
    }

    /**
     * 获取商家详情
     */
    public void get_commodity_type(){
        BecRetailer becRetailer = new BecRetailer();
        List<BecRetailer> list =becRetailer.dao().findAll();
        if (list==null){
            return;
        }
        renderJsonData(list);
    }


    /**
     * 获取特价下拉数据源
     */
    public void get_commodity_status(){
        List<OptionBean> options=new ArrayList<OptionBean>();
        options.add(new OptionBean("普通商品",0));
        options.add(new OptionBean("特价商品",1));
        options.add(new OptionBean("待审核商品",2));
        renderJsonData(options);
    }

    /**
     * 更新商品图片
     */
    @CheckPermission(PermissionKey.SPECIAL_COMMODITY_MODIFY)
    public  void update_option_images(){
        renderJsonData(optionService.update_option_images(getModel(BecRetailerCommodityOption.class,"BecOption")));
    }
}
